package com.sparrow.manage.index;

import com.sparrow.config.EnvironmentVariables;
import com.sparrow.manage.access.AccessPVService;
import com.sparrow.manage.article.ArticleService;
import com.sparrow.manage.comment.Comment;
import com.sparrow.manage.comment.CommentService;
import com.sparrow.manage.company.Company;
import com.sparrow.manage.company.CompanyService;
import com.sparrow.manage.monitor.AccessMonitor;
import com.sparrow.manage.user.UserService;
import com.sparrow.manage.util.FileUtil;
import com.sparrow.manage.util.SystemUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(EnvironmentVariables.MANAGE_ACCESS_PREFIX)
public class ManageIndexController {

    private static final Logger logger = LoggerFactory.getLogger(ManageIndexController.class);

    private static final String ipAddress = SystemUtils.ipAddress();

    private static final String javaInfo = SystemUtils.javaInfo();

    private static final String osInfo = SystemUtils.operationSystem();

    @Autowired
    private CommentService commentService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private AccessPVService accessPVService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String defaultPage(){
        return "redirect:index";
    }


    @RequestMapping("index")
    public String index(Model model){
        Company company = companyService.queryOne();
        model.addAttribute("icpNumber", company.getIcpNumber());
        model.addAttribute("companyName", company.getSname());
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "index";
    }

    @RequestMapping("welcome")
    public String welcome(Model model, HttpServletRequest request){
        // 系统信息
        model.addAttribute("ipAddress", ipAddress);
        model.addAttribute("osInfo", osInfo);
        model.addAttribute("javaInfo", javaInfo);
        model.addAttribute("accessIP", getAccessIpAddress(request));

        // 留言
        List<Comment> comments = commentService.queryByType(Comment.CommentType.NET_WORD, 5);
        model.addAttribute("comments", comments);

        // 文章数
        long articleTotal = articleService.queryTotal();
        model.addAttribute("articleTotal", articleTotal);

        // 留言数
        long commentTotal = commentService.queryTotal();
        model.addAttribute("commentTotal", commentTotal);

        // 访问量
        model.addAttribute("accessNum", AccessMonitor.AccessMonitorHolder.INSTANCE.getTotalAccess());

        // 活跃访问数
//        model.addAttribute("liveNum", AccessMonitor.AccessMonitorHolder.INSTANCE.getLiveAccess());

        // 前台页面数
        int pageCount = FileUtil.fileCount(EnvironmentVariables.getPortalStaticPublishPath(), EnvironmentVariables.TEMPLATE_SUFFIX);
        model.addAttribute("pageCount", pageCount);

        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "welcome";
    }

    @RequestMapping("diskSpaceInfo")
    @ResponseBody
    public SystemUtils.DiskSpaceInfo diskSpaceInfo(){
        return SystemUtils.getDiskSpaceInfo();
    }

    /**
     * 获取访问者的IP地址
     * @param request
     * @return
     */
    private String getAccessIpAddress(HttpServletRequest request){
        String refAddress = request.getHeader("x-forwarded-for");
        if(StringUtils.isNotBlank(refAddress)){
            return refAddress;
        }else{
            return request.getRemoteAddr();
        }
    }
}
